package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.PickUp;
import com.xmy.cultivate.service.IPickUpService;
import com.xmy.cultivate.mapper.RecordCourseMapper;
import com.xmy.cultivate.service.IGradeService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 家长接送信息 前端控制器
 * </p>
 *
 * @author Coly
 * @since 2023-04-27
 */
@RestController
@RequestMapping("/api/pickUp")
@Api(tags = "家长接送信息控制器")
@Scope("prototype")
public class PickUpController {

    @Autowired
    IPickUpService pickUpService;

    @Autowired
    RecordCourseMapper recordCourseMapper;

    @Autowired
    IGradeService iGradeService;


    @GetMapping("getPickUpByStudentId")
    @ApiOperation("根据学生id获取接送信息")
    @ApiImplicitParam(name = "studentId" , value = "学生id" , required = true , dataType = "String")
    public Result getPickUpByStudentId(@RequestParam("studentId")String studentId){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);
        PickUp pickUp = pickUpService.getOne(queryWrapper);
        return new Result(Code.OK,pickUp,"接送信息获取完成~");
    }

    @GetMapping("getPickUpPage")
    @ApiOperation("分页查询家长接送信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum" , value = "页码" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "pageSize" , value = "页尺寸" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "schoolId" , value = "校区" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "yearPart" , value = "年份" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "quarterNum" , value = "学期" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "yearClassId" , value = "年级" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "gradeId" , value = "班级id" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "pickUpType" , value = "接送方式" , required = false , dataType = "String")
    })
    public Result getPickUpPage(@RequestParam Map reMap){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
        IPage<PickUp> iPage = new Page<>(pageNum,pageSize);

        String schoolId = (String) reMap.get("schoolId");
        String yearPart = (String) reMap.get("yearPart");
        String quarterNum = (String) reMap.get("quarterNum");
        String yearClassId = (String) reMap.get("yearClassId");
        String gradeId = (String) reMap.get("gradeId");
        String pickUpType = (String) reMap.get("pickUpType");

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(yearPart),"year_part",yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum),"quarter_num",quarterNum);
        queryWrapper.eq(StringUtils.isNotBlank(yearClassId),"year_class_id",yearClassId);
        queryWrapper.eq(StringUtils.isNotBlank(gradeId),"grade_id",gradeId);
        queryWrapper.eq(StringUtils.isNotBlank(pickUpType),"pickup_type",pickUpType);

        pickUpService.page(iPage,queryWrapper);
        return new Result(Code.OK,iPage,"学校接送信息分页获取完成~");
    }

    @ApiOperation("接送信息统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum" , value = "页码" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "pageSize" , value = "分页尺寸" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "schoolId" , value = "校区" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "yearPart" , value = "年份" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "quarterNum" , value = "学期" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "yearClassId" , value = "年级" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "subjectsId" , value = "科目" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "type" , value = "类型" , required = false , dataType = "int")
    })
    @GetMapping("PickUpList")
    public Result PickUpList(@RequestParam Map reMap){
        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
        IPage<PickUp> iPage = new Page<>(pageNum,pageSize);

        String schoolId = (String) reMap.get("schoolId");
        String subjectsId = (String) reMap.get("subjectsId");
        String yearPart = (String) reMap.get("yearPart");
        String quarterNum = (String) reMap.get("quarterNum");
        String yearClassId = (String) reMap.get("yearClassId");
        String types = (String) reMap.get("type");
        Integer type = Integer.valueOf(types);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(subjectsId),"subjects_id",subjectsId);
        queryWrapper.eq(StringUtils.isNotBlank(yearPart),"year_part",yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum),"quarter_num",quarterNum);
        queryWrapper.eq(StringUtils.isNotBlank(yearClassId),"year_class_id",yearClassId);

        List<Long> adminIdList = recordCourseMapper.selectAdminId();

        if(type == 0){
            queryWrapper.groupBy("teacher_id");
        }else if(type == 1){
            queryWrapper.groupBy("teacher_id","school_id");
        }else if(type == 2){
            queryWrapper.in("teacher_id",adminIdList);
            queryWrapper.groupBy("teacher_id");
        }else if(type ==3){
            queryWrapper.groupBy("school_id");
        }
        pickUpService.sumPickUp(iPage,queryWrapper);
        return new Result(Code.OK,iPage,"老师名下家长接送信息统计完成");
    }

    @ApiOperation("个人-家长接送信息详细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId" , value = "老师id" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "gradeId" , value = "班级id" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "pickupType" , value = "家长接送类型" , required = false , dataType = "int")
    })
    @GetMapping("pickUpDetailOneself")
    public Result pickUpDetailOneself(@RequestParam("teacherId") String teacherId,
                                      @RequestParam("gradeId") String gradeId,
                                      @RequestParam("pickUpType") String pickUpType){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(teacherId),"teacher_id",teacherId);
        queryWrapper.eq(StringUtils.isNotBlank(gradeId),"grade_id",gradeId);
        queryWrapper.eq(StringUtils.isNotBlank(pickUpType),"pickup_type",pickUpType);
        List<PickUp> pickUps = pickUpService.list(queryWrapper);
        return new Result(Code.OK,pickUps,"个人-家长接送信息遍历完成");
    }

    @ApiOperation("校区老师-家长接送信息详细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId" , value = "老师id" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "schoolId" , value = "校区id" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "gradeId" , value = "班级id" , required= false , dataType = "String"),
            @ApiImplicitParam(name = "pickUpType" , value = "家长接送类型" , required = false , dataType = "int")
    })
    @GetMapping("pickUpDetailSTeacher")
    public Result pickUpDetailSTeacher(@RequestParam("teacherId") String teacherId,
                                      @RequestParam("schoolId") String schoolId,
                                      @RequestParam("gradeId") String gradeId,
                                      @RequestParam("pickUpType") String pickUpType){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(teacherId),"teacher_id",teacherId);
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(gradeId),"grade_id",gradeId);
        if(Integer.valueOf(pickUpType) == 0){
            queryWrapper.eq("pickup_type",0);
        }else{
            queryWrapper.ne("pickup_type",0);
        }

        List<PickUp> pickUps = pickUpService.list(queryWrapper);
        return new Result(Code.OK,pickUps,"校区老师-家长接送信息遍历完成");
    }

    @ApiOperation("校区-接送信息详细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId" , value = "校区id" , required = true , dataType = "String"),
            @ApiImplicitParam(name = "gradeId" , value = "班级id" , required= false , dataType = "String"),
            @ApiImplicitParam(name = "pickupType" , value = "家长接送类型" , required = false , dataType = "int")
    })
    @GetMapping("pickUpDetailSchool")
    public Result pickUpDetailSchool(@RequestParam("schoolId") String schoolId,
                                       @RequestParam("gradeId") String gradeId,
                                       @RequestParam("pickUpType") String pickUpType){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(gradeId),"grade_id",gradeId);
        queryWrapper.eq(StringUtils.isNotBlank(pickUpType),"pickup_type",pickUpType);
        List<PickUp> pickUps = pickUpService.list(queryWrapper);
        return new Result(Code.OK,pickUps,"校区-家长接送信息遍历完成");
    }


    @GetMapping("getGrade")
    @ApiOperation("根据老师id或者学区id获取班级信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId" , value = "校区id" , required = false , dataType = "String"),
            @ApiImplicitParam(name = "teacherId" , value= "老师id" , required = false , dataType = "String")
    })
    public Result getGrade(@RequestParam("schoolId")String schoolId,
                                    @RequestParam("teacherId")String teacherId){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(teacherId),"teacher_id",teacherId);
        queryWrapper.eq("year_part",2023);
        queryWrapper.eq("status",1);
        List<Grade> gradeList = iGradeService.list(queryWrapper);
        return new Result(Code.OK,gradeList,"班级信息列表获取完成~");
    }

}
